
correlation_results <- estimation_data %>%
  group_by(validated_election_outcome, A) %>%
  summarise(
    cor_test = list(cor.test(avg_score_trust_elect, avg_score_democ_support)),
    .groups = "drop"
  ) %>%
  mutate(
    estimate = map_dbl(cor_test, ~ .x$estimate),
    conf.low = map_dbl(cor_test, ~ .x$conf.int[1]),
    conf.high = map_dbl(cor_test, ~ .x$conf.int[2])
  )


p <-
  ggplot(correlation_results, aes(x = A, y = estimate, group = validated_election_outcome, color = validated_election_outcome)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = 0.5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major.x = element_blank(),
    axis.title.x = element_blank(),
    strip.background = element_blank(),
    legend.title = element_blank(),
    axis.line = element_line(colour = "black"),
    panel.border = element_blank(),
    legend.position = "bottom"
  ) +
  ylab("Pearson Correlation Coefficient\n(Trust in Elections, Support for Democracy)") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  scale_x_continuous(
    breaks = c(1, 2, 3),
    labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")
  )

ggsave(plot = p, "./outputs/figures/figure_a7.pdf", width =5, height = 4)
ggsave(plot = p, "./outputs/figures/figure_a7.eps", device = "eps", width =5, height = 4)

